fix(bedrock): handle common image format aliases in BedrockImageProce…#24269
fix(bedrock): handle common image format aliases in BedrockImageProce…#24269longzhihun wants to merge 1 commit intoBerriAI:mainfrom
Conversation
…ssor Feishu (Lark) and other platforms may send images with MIME type `image/jpg` instead of `image/jpeg`. The Bedrock converse API validator rejects `jpg` even though it is the same format as `jpeg`. Add a format alias mapping to normalize common equivalent extensions before validation (`jpg` → `jpeg`, `mpg` → `mpeg`), preventing unnecessary 500 errors for valid media types. Made-with: Cursor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR fixes a real bug where Key changes:
Issues found:
Confidence Score: 3/5
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/prompt_templates/factory.py | Adds jpg→jpeg and mpg→mpeg aliases in _validate_format. The jpg→jpeg alias correctly fixes a real bug. However, mpg was already in get_supported_video_types(), so the mpg→mpeg alias introduces a silent behavior change (return value changes from "mpg" to "mpeg") for an input that was already working. |
| tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py | Adds a new test test_bedrock_validate_format_resolves_common_aliases covering alias resolution and updates test_bedrock_validate_format_image_or_video to expect mpeg for mpg input. Tests are pure unit tests with no network calls. Contains a placeholder issue URL (XXXXX) in a docstring. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["_validate_format(mime_type, image_format)"] --> B{Is document?\nmime_type starts with\n'application' or 'text'}
B -- Yes --> C["_get_document_format()"]
B -- No --> D["NEW: Apply format_aliases\n{'jpg':'jpeg', 'mpg':'mpeg'}"]
D --> E{image_format in\nsupported_image_and_video_formats?}
E -- No --> F["raise ValueError\nUnsupported image format"]
E -- Yes --> G["return image_format\n(normalized)"]
subgraph supported_image_and_video_formats
H["Images: png, jpeg, gif, webp"]
I["Videos: mp4, mov, mkv, webm,\nflv, mpeg, mpg, wmv, 3gp"]
end
style D fill:#ffe0b2,color:#000
style I fill:#fff9c4,color:#000
note1["⚠️ 'mpg' already in supported list\nbefore alias lookup"]
Last reviewed commit: "fix(bedrock): handle..."
|
|
| format_aliases = {"jpg": "jpeg", "mpg": "mpeg"} | ||
| image_format = format_aliases.get(image_format, image_format) |
There was a problem hiding this comment.
mpg was already a supported format — unnecessary behavior change
get_supported_video_types() in converse_transformation.py already returns "mpg" in its list:
def get_supported_video_types(self) -> List[str]:
return ["mp4", "mov", "mkv", "webm", "flv", "mpeg", "mpg", "wmv", "3gp"]This means "mpg" was already passing the if image_format not in supported_image_and_video_formats check correctly and was returned unchanged as "mpg". By adding the mpg → mpeg alias, this PR silently changes the return value of _validate_format("video/mpg", "mpg") from "mpg" to "mpeg". Any caller that previously relied on "mpg" being passed through (e.g., as the format field in a BedrockVideoBlock) will now see "mpeg" instead.
The real bug — and the correct, minimal fix — only affects "jpg", which is genuinely absent from get_supported_image_types() (which returns ["png", "jpeg", "gif", "webp"]).
Consider either removing the "mpg" entry from the alias dict, or first removing "mpg" from get_supported_video_types() if the intent is to consolidate on "mpeg" as the canonical form:
| format_aliases = {"jpg": "jpeg", "mpg": "mpeg"} | |
| image_format = format_aliases.get(image_format, image_format) | |
| format_aliases = {"jpg": "jpeg"} | |
| image_format = format_aliases.get(image_format, image_format) |
| Regression test for: https://github.com/BerriAI/litellm/issues/XXXXX | ||
| """ |
There was a problem hiding this comment.
Placeholder issue URL should be removed
The docstring references a placeholder GitHub issue URL (https://github.com/BerriAI/litellm/issues/XXXXX) that was never filled in. This will confuse future contributors trying to find the original context.
| Regression test for: https://github.com/BerriAI/litellm/issues/XXXXX | |
| """ | |
| Regression test for the bug where `jpg` and `mpg` format aliases were | |
| not accepted by _validate_format. |
| ######################################################### | ||
| # Check if image_format is an image or video | ||
| ######################################################### | ||
| format_aliases = {"jpg": "jpeg", "mpg": "mpeg"} |
There was a problem hiding this comment.
format_aliases dict re-created on every invocation
_validate_format is called on every image/video request. Allocating a new dict object on each call is a minor but unnecessary overhead. Since the alias map is static, it should be defined as a class-level constant:
class BedrockImageProcessor:
_FORMAT_ALIASES: Dict[str, str] = {"jpg": "jpeg"}
...Then use self._FORMAT_ALIASES.get(image_format, image_format) (or BedrockImageProcessor._FORMAT_ALIASES.get(...) since it's a @staticmethod).
Summary
BedrockImageProcessor._validate_format()rejectsjpgandmpgformats withValueError: Unsupported image format, even though they are standard aliases forjpegandmpegrespectively.format_aliasesdictionary to normalize common format aliases before validation. This mapsjpg→jpegandmpg→mpeg.test_bedrock_validate_format_resolves_common_aliasesto verify alias resolution, canonical format passthrough, and unsupported format rejection. Updated existingtest_bedrock_validate_format_image_or_videoto reflectmpg→mpegnormalization.Motivation
When users send
.jpgimages (the most common JPEG file extension) through Bedrock-backed models via LiteLLM, the request fails because the format validator only recognizesjpegas the canonical name. The JPEG standard uses both.jpgand.jpegextensions interchangeably, and similarly MPEG videos use both.mpgand.mpeg. This fix ensures both common extensions are accepted.Changes
litellm/litellm_core_utils/prompt_templates/factory.py: Added format alias resolution in_validate_format()tests/test_litellm/.../test_litellm_core_utils_prompt_templates_factory.py: Added new test + updated existing testTest plan
test_bedrock_validate_format_resolves_common_aliasescovering:jpg→jpegalias resolutionmpg→mpegalias resolutionjpeg,mpeg) still work unchangedValueErrortest_bedrock_validate_format_image_or_videoto expectmpegwhen input ismpgType
🐛 Bug Fix